package com.chinatelecom.ifree.business.dao.base;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chinatelecom.ifree.business.configcenter.helper.MysqlJdbcHelper;

/**
 * dbcp pool
 * 
 * @author haoxw
 * @since 2014/7/10
 */
public class DbcpPool {
	public static final Logger log = LoggerFactory.getLogger(DbcpPool.class);
	private static final String configFile = "db.properties";

	private static DataSource dataSource = null;
	private static DbcpPool instance = null;

	public static DbcpPool getInstance() {
		synchronized (DbcpPool.class) {
			if (instance == null) {
				try {
					log.info("DbcpPool getInstance initilize====");
					instance = new DbcpPool();
					instance.init();
				} catch (Exception e) {
					log.error("getInstance", e);
				}
			}
		}
		return instance;

	}

	public void init() {
		Properties dbProperties = new Properties();
		try {
			dbProperties.load(DbcpPool.class.getClassLoader().getResourceAsStream(configFile));
			String findSerice = dbProperties.getProperty("findService") ;
			if(!"direct".equals(findSerice)){
				dbProperties.setProperty("driverClassName", "com.mysql.jdbc.Driver");
				dbProperties.setProperty("url", MysqlJdbcHelper.getInstance().getJdbcUrl());
				dbProperties.setProperty("username", MysqlJdbcHelper.getInstance().getUsername());
				dbProperties.setProperty("password", MysqlJdbcHelper.getInstance().getPasswd());
				dbProperties.setProperty("maxActive", "100");
				dbProperties.setProperty("maxIdle", "100");
				dbProperties.setProperty("maxWait", "1000");
				dbProperties.setProperty("testOnBorrow", "true");
				dbProperties.setProperty("removeAbandoned", "true");
				dbProperties.setProperty("removeAbandonedTimeout", "180");
			}
			
			dataSource = BasicDataSourceFactory.createDataSource(dbProperties);
		} catch (Exception e) {
			log.error("init pool fail:", e);
		}
	}

	private DbcpPool() {
	}

	/**
	 * 获取链接，用完后记得关闭
	 * 
	 * @see {@link DBManager#closeConn(Connection)}
	 * @return
	 */
	public Connection getConn() {
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			log.error("getConn fail:", e);
		}
		return conn;
	}

	/**
	 * 关闭执行查询操作的连接
	 */
	public void closeConnection(ResultSet resultSet, Statement statement, Connection connection) {
		try {
			if (resultSet != null) {
				resultSet.close();
				resultSet = null;
			}
			if (statement != null) {
				statement.close();
				statement = null;
			}
			if (connection != null) {
				connection.close();
				connection = null;
			}
		} catch (SQLException e) {
			log.error("", e);
		}
	}

	/**
	 * 关闭执行更新操作的连接
	 */
	public void closeConnection(Statement statement, Connection connection) {
		try {
			if (statement != null) {
				statement.close();
				statement = null;
			}
			if (connection != null) {
				connection.close();
				connection = null;
			}
		} catch (SQLException e) {
			log.error("", e);
		}
	}
}
